import {
    GetScoreTaskList,
    SignIn,
    GetRandomTemplate,
    GetRandomNews,
    GetRandomWork,
    SetTaskScore,
} from "~/api";
import { aesEncrypt } from "~/utils/encrypt";

export const useContent = () => {
    const dialogStore = useDialogStore();
    const userStore = useUserStore();
    const list = ref([]);

    const onClose = () => {
        dialogStore.isTaskListDialogVisible = false;
    };

    const GetList = async () => {
        const res = await GetScoreTaskList();
        list.value = res.data;
    };

    const goRandomNews = async () => {
        const res = await GetRandomNews();
        navigateTo("/news/" + res.data.id + ".html", { open: true });
        return res.data;
    };

    const goRandomTemplate = async () => {
        const res = await GetRandomTemplate();

        // 判断类型
        let origin = GetTemplateLink3(res.data.templatestype_id);

        const search =
            "tid=" +
            res.data.id +
            "&type=template&uid=" +
            userStore.userInfo.id;
        const newUrl = `${origin}/?${aesEncrypt(search)}`;
        navigateTo(newUrl, { external: true, open: true });
        return res.data;
    };

    const goRandomWorks = async () => {
        const res = await GetRandomWork();
        navigateTo(res.data.url, { external: true, open: true });
        return res.data;
    };

    const onGoToScoreAction = async (item) => {
        const { code, user_times, times } = item;

        if (user_times < times) {
            switch (code) {
                case 1000:
                    await SignIn();
                    dialogStore.isSignInDialogVisible = true;
                    GetList();
                    break;
                case 1001:
                    // 浏览任意新闻
                    // await goRandomNews();
                    // await GetList();
                    navigateTo("/news.html");
                    dialogStore.isTaskListDialogVisible = false;
                    break;
                case 1002:
                    // 浏览任意模版
                    goRandomTemplate().then(async (item) => {
                        await SetTaskScore({
                            code: 1002,
                            _id: item.id,
                        });
                        await GetList();
                    });
                    break;
                case 1003:
                    // 浏览任意作品
                    // goRandomWorks().then(async (item) => {
                    //     await SetTaskScore({
                    //         code: 1003,
                    //         _id: item.id,
                    //     });
                    //     await GetList();
                    // });
                    navigateTo("/worksSquare.html");
                    dialogStore.isTaskListDialogVisible = false;
                    break;
                case 1004:
                    // 分享任意作品
                    navigateTo("/worksSquare.html");
                    dialogStore.isTaskListDialogVisible = false;
                    break;
                case 1005:
                    // 点赞任意作品
                    navigateTo("/worksSquare.html");
                    dialogStore.isTaskListDialogVisible = false;
                    break;
                case 1006:
                    // 收藏任意作品
                    navigateTo("/worksSquare.html");
                    dialogStore.isTaskListDialogVisible = false;
                    break;
                case 1007:
                    // 评论任意作品
                    navigateTo("/worksSquare.html");
                    dialogStore.isTaskListDialogVisible = false;
                    break;
                case 1008:
                    // 创建任意作品
                    navigateTo("/template.html");
                    dialogStore.isTaskListDialogVisible = false;
                    break;
                case 1009:
                    // 发布任意作品
                    navigateTo("/template.html");
                    dialogStore.isTaskListDialogVisible = false;
                    break;
                case 1010:
                    // 补全个人资料
                    navigateTo("/admin/account.html");
                    dialogStore.isTaskListDialogVisible = false;
                    break;
                case 1011:
                    // 开通任意会员
                    navigateTo("/template.html");
                    dialogStore.isTaskListDialogVisible = false;
                    break;
                default:
                    Message().error("暂未开放！");
            }
        }
    };

    watch(
        () => dialogStore.isTaskListDialogVisible,
        () => {
            if (dialogStore.isTaskListDialogVisible) {
                GetList();
            } else {
                list.value = [];
            }
        }
    );

    return {
        list,
        onClose,
        onGoToScoreAction,
    };
};
